package com.ts.opengl.simpleDraw

import android.content.Context
import android.graphics.BitmapFactory
import android.opengl.GLES20.GL_CLAMP_TO_EDGE
import android.opengl.GLES20.GL_COLOR_BUFFER_BIT
import android.opengl.GLES20.GL_DEPTH_BUFFER_BIT
import android.opengl.GLES20.GL_FLOAT
import android.opengl.GLES20.GL_LINEAR
import android.opengl.GLES20.GL_NONE
import android.opengl.GLES20.GL_STENCIL_BUFFER_BIT
import android.opengl.GLES20.GL_TEXTURE0
import android.opengl.GLES20.GL_TEXTURE_2D
import android.opengl.GLES20.GL_TEXTURE_MAG_FILTER
import android.opengl.GLES20.GL_TEXTURE_MIN_FILTER
import android.opengl.GLES20.GL_TEXTURE_WRAP_S
import android.opengl.GLES20.GL_TEXTURE_WRAP_T
import android.opengl.GLES20.glActiveTexture
import android.opengl.GLES20.glBindTexture
import android.opengl.GLES20.glClear
import android.opengl.GLES20.glClearColor
import android.opengl.GLES20.glEnableVertexAttribArray
import android.opengl.GLES20.glGetUniformLocation
import android.opengl.GLES20.glTexParameterf
import android.opengl.GLES20.glTexParameteri
import android.opengl.GLES20.glUniform1i
import android.opengl.GLES20.glUniformMatrix4fv
import android.opengl.GLES20.glVertexAttribPointer
import android.opengl.GLES30
import android.opengl.GLES30.glBindVertexArray
import android.opengl.GLES30.glDrawBuffers
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.opengl.Matrix
import android.util.Log
import com.ts.audiocamera.R
import com.ts.opengl.IInterface.TransformRender
import com.ts.opengl.OpenGLUtils
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class DeepRender(var context: Context) : TransformRender() {





    override fun UpdateTransformMatrix(
        rotateX: Float,
        rotateY: Float,
        scaleX: Float,
        scaleY: Float
    ) {
        super.UpdateTransformMatrix(rotateX, rotateY, scaleX, scaleY)

    }


    var mTexture=IntArray(1)
    var vbo=IntArray(1)
    var vao=IntArray(1)

    var vertices = floatArrayOf(
        //position                       //texture coord            //normal
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,

        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,

        -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f,

        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,

        -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,

        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
    )
    val textBuffer:FloatBuffer by lazy{
        openglUtil.createFloatBuffer(vertices)
    }
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        program=openglUtil.createProgram(context, R.raw.depth_ver,R.raw.depth_frag)
        Log.e(TAG,GLES30.glGetError().toString()+"-2321--")
        //生成纹理
        GLES30.glGenTextures(mTexture.size,mTexture,0)
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,mTexture[0])
        Log.e(TAG,GLES30.glGetError().toString()+"-2321--")
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        Log.e(TAG,GLES30.glGetError().toString()+"-2321--221")
        glBindTexture(GL_TEXTURE_2D, GL_NONE);


        GLES30.glGenBuffers(vbo.size,vbo,0)
        Log.e(TAG,GLES30.glGetError().toString()+"-2322--")
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER,vbo[0])
        Log.e(TAG,GLES30.glGetError().toString()+"-2322--")
        GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER,textBuffer.capacity()*4,textBuffer,GLES30.GL_STATIC_DRAW)

        GLES30.glGenVertexArrays(vao.size,vao,0)
        GLES30.glBindVertexArray(vao[0])
        Log.e(TAG,GLES30.glGetError().toString()+"-2323--")

        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vbo[0]);
        GLES30.glEnableVertexAttribArray(0);
        GLES30.glVertexAttribPointer(0, 3, GL_FLOAT,
            false, 8 * 4, 0);
        glEnableVertexAttribArray(1);
        GLES30.glVertexAttribPointer(1 ,2, GL_FLOAT,
            false, 8 * 4, 3*4);
        glEnableVertexAttribArray(2);
        GLES30.glVertexAttribPointer(2 ,3, GL_FLOAT,
            false, 8 * 4, 5*4);

        GLES30.glBindTexture(GLES30.GL_ARRAY_BUFFER,GLES30.GL_NONE)
        GLES30.glBindVertexArray(GLES30.GL_NONE)

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D,mTexture[0])
        Log.e(TAG,GLES30.glGetError().toString()+"-232--")
        val bitmap=BitmapFactory.decodeResource(context.resources,R.drawable.view)
        GLUtils.texImage2D(GL_TEXTURE_2D,0,bitmap,0)
        glBindTexture(GL_TEXTURE_2D, GL_NONE);
        bitmap.recycle()



    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {

    }



    override fun onDrawFrame(gl: GL10?) {
        glClear(GL_COLOR_BUFFER_BIT or
                GL_DEPTH_BUFFER_BIT or
                GL_STENCIL_BUFFER_BIT)

        glClearColor(1.0f,1.0f,1.0f,1.0f)

        GLES30.glEnable( GLES30.GL_DEPTH_TEST)

        GLES30.glUseProgram(program)

        glBindVertexArray(vao[0]);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture[0]);
        glUniform1i(glGetUniformLocation(program, "s_TextureMap"), 0);
        UpdateMatrix(m_AngleX,m_AngleX)
        glUniformMatrix4fv(glGetUniformLocation(program,"u_MVPMatrix"), 1, false,mMVPMatrix,0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,0,36)
        Log.e(TAG,GLES30.glGetError().toString()+"-32--")
    }

    private  val TAG = "DeepRender"

    fun UpdateMatrix(angleX:Int,angleY:Int){

        val angleX1=angleX%360.0
        val angleY1=angleY%360.0
        Log.e(TAG, "旋转角度3232 $angleX1 $angleY1")
        val radiansX=(Math.PI/180.0f*(angleX1)).toFloat()
        val radiansY=(Math.PI/180.0f*(angleY1)).toFloat()
        Log.e(TAG, "旋转角度3232 $radiansX $radiansY")

        //计算宽高比
        val ratio = android.R.attr.width.toFloat() / android.R.attr.height

        //设置透视投影
        Matrix.perspectiveM(mProjectMatrix, 0,45.0f,ratio,0.1f,200.0f)

        //设置相机位置
        Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, 3f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
        Log.e(TAG, "旋转角度322232 $m_ScaleX $m_ScaleX  $radiansX  $radiansY")
        Matrix.scaleM(model,0,m_ScaleX,m_ScaleX,1.0f)
        Matrix.rotateM(model,0, radiansX,1.0f,0.0f,0.0f)
        Matrix.rotateM(model,0, radiansY,0.0f,1.0f,0.0f)
        Matrix.translateM(model, 0,1.0f, 2.0f, 3.0f)
        //计算变换矩阵
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectMatrix, 0, mViewMatrix, 0)
        Matrix.multiplyMM(mMVPMatrix, 0, mMVPMatrix, 0, model, 0)
    }

}